package com.greensam.pixelengine.controller;

import com.greensam.pixelengine.annotation.AuthCheck;
import com.greensam.pixelengine.api.aliyunai.model.CreateOutPaintingTaskResponse;
import com.greensam.pixelengine.api.aliyunai.model.GetOutPaintingTaskResponse;
import com.greensam.pixelengine.api.imagesearch.model.ImageSearchResult;
import com.greensam.pixelengine.constant.SpaceUserPermissionConstant;
import com.greensam.pixelengine.facade.PictureFacadeService;
import com.greensam.pixelengine.facade.UserFacadeService;
import com.greensam.pixelengine.manager.auth.annotation.SaSpaceCheckPermission;
import com.greensam.pixelengine.pojo.base.BaseResponse;
import com.greensam.pixelengine.pojo.base.DeleteRequest;
import com.greensam.pixelengine.pojo.base.PaginationResponse;
import com.greensam.pixelengine.pojo.emuns.RoleEnum;
import com.greensam.pixelengine.pojo.entity.UserPo;
import com.greensam.pixelengine.pojo.vo.picture.request.*;
import com.greensam.pixelengine.pojo.vo.picture.response.PictureVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 图片
 *
 * @author Macro_Ray
 * @since 2025/6/13 16:38
 */
@Tag(name = "PictureController", description = "图片模块")
@RestController
@RequestMapping("/picture")
@RequiredArgsConstructor
public class PictureController {

    private final UserFacadeService userFacadeService;

    private final PictureFacadeService pictureFacadeService;

    @Operation(summary = "主页 - 分页获取图片")
    @PostMapping("/page")
    public BaseResponse<PaginationResponse<PictureVo>> pagePicture(HttpServletRequest httpServletRequest,
                                                                   @RequestBody PicturePageRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.pagePicture(request, loginUser.getId()));
    }

    @Deprecated
    @Operation(summary = "主页 - 分页获取图片（缓存）")
    @PostMapping("/cache/page")
    public BaseResponse<PaginationResponse<PictureVo>> pagePictureWithCache(@RequestBody PicturePageRequest request) {
        return BaseResponse.success(pictureFacadeService.pagePictureWithCache(request));
    }

    @Operation(summary = "主页 - 获取图片详情")
    @GetMapping("/get")
    public BaseResponse<PictureVo> getPictureDetail(HttpServletRequest httpServletRequest,
                                                    @RequestParam("id") @Parameter(description = "图片ID") Long id) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.getPictureDetail(id, loginUser.getId()));
    }

    @Operation(summary = "主页 - 以图搜图")
    @PostMapping("/search/picture")
    public BaseResponse<List<ImageSearchResult>> searchPictureByPicture(@RequestBody SearchPictureByPictureRequest request) {
        return BaseResponse.success(pictureFacadeService.searchPictureByPicture(request));
    }

    @Operation(summary = "主页 - 颜色搜图")
    @PostMapping("/search/color")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<PictureVo>> searchPictureByColor(HttpServletRequest httpServletRequest,
                                                              @RequestBody SearchPictureByColorRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.searchPictureByColor(request, loginUser.getId()));
    }


    @Operation(summary = "图片管理 - 上传图片")
    @PostMapping("/manager/upload")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVo> uploadPicture(@RequestPart("file") MultipartFile file,
                                                 HttpServletRequest httpServletRequest,
                                                 PictureUploadRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.uploadPicture(file, request, loginUser.getId(), loginUser.getName()));
    }

    @Operation(summary = "图片管理 - URL上传图片")
    @PostMapping("/manager/upload/url")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVo> uploadPictureByUrl(HttpServletRequest httpServletRequest,
                                                      @RequestBody PictureUploadRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        String fileUrl = request.getFileUrl();
        return BaseResponse.success(pictureFacadeService.uploadPicture(fileUrl, request, loginUser.getId(), loginUser.getName()));
    }

    @Operation(summary = "图片管理 - 批量上传图片")
    @PostMapping("/manager/upload/batch")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<Integer> batchUploadPicture(HttpServletRequest httpServletRequest, @RequestBody PictureBatchUploadRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.batchUploadPicture(request, loginUser.getId(), loginUser.getName()));
    }

    @Operation(summary = "图片管理 - 删除图片")
    @PostMapping("/manager/delete")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_DELETE)
    public BaseResponse<Boolean> deletePicture(HttpServletRequest httpServletRequest, @RequestBody @Valid DeleteRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.deletePicture(request, loginUser.getId()));
    }

    @Operation(summary = "图片管理 - 更新图片")
    @PostMapping("/manager/update")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<Boolean> updatePicture(HttpServletRequest httpServletRequest, @RequestBody @Valid PictureUpdateRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.updatePicture(request, loginUser.getId()));
    }

    @Operation(summary = "图片管理 - 编辑图片")
    @PostMapping("/manager/edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPicture(HttpServletRequest httpServletRequest, @RequestBody @Valid PictureEditRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.editPicture(request, loginUser.getId()));
    }

    @Operation(summary = "图片管理 - 分页获取图片")
    @PostMapping("/manager/page")
    public BaseResponse<PaginationResponse<PictureVo>> pageManagerPicture(@RequestBody PictureManagerPageRequest request) {
        return BaseResponse.success(pictureFacadeService.pageManagerPicture(request));
    }

    @Operation(summary = "图片管理 - 审核图片")
    @PostMapping("/manager/review")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<Boolean> reviewPicture(HttpServletRequest httpServletRequest, @RequestBody PictureReviewRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.reviewPicture(request, loginUser.getId()));
    }

    @Operation(summary = "AI - 创建扩图任务")
    @PostMapping("/ai/create/outpainting/task")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<CreateOutPaintingTaskResponse> createPictureOutPaintingTask(HttpServletRequest httpServletRequest,
                                                                                    @RequestBody CreatePictureOutPaintingTaskRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(pictureFacadeService.createPictureOutPaintingTask(request, loginUser.getId()));
    }

    @Operation(summary = "AI - 查询扩图任务")
    @GetMapping("/ai/get/outpainting/task")
    public BaseResponse<GetOutPaintingTaskResponse> getPictureOutPaintingTask(@RequestParam("taskId") String taskId) {
        return BaseResponse.success(pictureFacadeService.getPictureOutPaintingTask(taskId));
    }
}
